﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data.Objects.DataClasses;
using System.Data.Services.Common;
using ADONETDataServicesExtension;
using ADONETDataServicesExtension.BLLUtils;

namespace BLL
{
    [DataServiceKey("Id")]
    [EFEntityType(TypeName = "EFEntities.Team", EntitySetName = "Teams")]
    public class Team : BaseEntityType<EFEntities.Team>
    {
        public Team() :
            base(() => new EFEntities.Team())
        {
        }
        public Team(EFEntities.Team team)
            : base(team)
        {
        }

        [EFEntityProperty]
        public Guid Id
        {
            get { return EFEntity.Id; }
            set { EFEntity.Id = value; }
        }

        [EFEntityProperty]
        public string Name
        {
            get { return EFEntity.Name; }
            set { EFEntity.Name = value; }
        }

        private TeamManager _manager;
        [EFEntityProperty]
        public TeamManager Manager
        {
            get
            {
                if (_manager == null)
                {
                    if (EFEntity.Manager != null)
                        _manager = new TeamManager(EFEntity.Manager);
                }
                return _manager;
            }
            set
            {
                _manager = value;
                if (value != null)
                    EFEntity.SetManagerReference(value.Id);
            }
        }

        private IEnumerable<Employee> _employees;
        [EFEntityProperty]
        public IEnumerable<Employee> Employees
        {
            get
            {
                if (_employees == null)
                    _employees = EFEntity.Employees.AsEnumerable().Select(e => new Employee(e));
                return _employees;
            }
        }
    }
}
